package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
* @author 牛y
* @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
* @createDate 2025-07-21 14:08:06
*/
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
    implements TbTypeService{

    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    HttpServletRequest request;


    @Autowired
    FastFileStorageClient fileStorageClient;

    @Override
    public R getTypeTree(PageInfoVo pageInfoVo) {
        List<TbType> tbTypes = tbTypeMapper.getTypeTree(pageInfoVo);
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeImages", type.getTypeImage());
            tree.putExtra("typeStatus", type.getTypeStatus());
        });
        return R.OK(treeList);
    }

    @Override
    public R uploadFile(MultipartFile file) {
        String extName = FileNameUtil.extName(file.getOriginalFilename());
        try {
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            return R.ERROR(500,"图片上传失败");
        }
    }

    @Override
    public R addType(TbType tbType, BindingResult result) {

        if(result.hasFieldErrors()){
            return R.ERROR(500,result.getFieldError().getDefaultMessage());
        }
        TbType type = tbTypeMapper.selectOne(new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, tbType.getTypeName()));
        if(type!=null){
            return R.ERROR(500,"类型名称已存在");
        }
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        String userName = body.get("userName", String.class);
        tbType.setCreateBy(userName);
        tbType.setCreateTime(new Date());
        tbTypeMapper.insert(tbType);
        return R.OK();
    }

    @Override
    public R selectType() {
        List<TbType> tbTypes = tbTypeMapper.selectList(null);
        List<Tree<String>> treeList = TreeUtil.build(tbTypes, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            tree.putExtra("label", type.getTypeName());
            tree.putExtra("value", type.getTypeId()+"");
        });
        return R.OK(treeList);
    }
}




